/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lucava.boatskeeper.client.view;

import org.lucava.lib.client.view.MvcView;
import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.lucava.boatskeeper.client.event.category.DeleteCategoriesEvent;
import org.lucava.boatskeeper.client.event.category.EditCategoryEvent;
import org.lucava.boatskeeper.shared.dto.CategoryDto;

/**
 *
 * @author glenn
 */
public class CategoryListView extends MvcView {

    private final Button addButton;
    private final Button deleteButton;
    private final MultiSelectionModel<CategoryDto> selectionModel;
    private final CellTable<CategoryDto> table;
    private final Column<CategoryDto, String> editColumn;
    private final SimplePager simplePager;
    private final ListDataProvider<CategoryDto> dataProvider;

    public CategoryListView(EventBus eventBus) {
        super(eventBus);

        DecoratorPanel panelDecorator = new DecoratorPanel();
        panelDecorator.setWidth("50em");

        // Create the menu

        HorizontalPanel hPanel = new HorizontalPanel();
        hPanel.setBorderWidth(0);
        hPanel.setSpacing(2);
        hPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);

        addButton = new Button("Ajouter une catégorie", new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                getEventBus().fireEvent(new EditCategoryEvent(null));
            }
        });

        deleteButton = new Button("Supprimer les catégories sélectionnés", new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                getEventBus().fireEvent(new DeleteCategoriesEvent(getSelectedRows()));
            }
        });

        hPanel.add(addButton);
        hPanel.add(deleteButton);



        table = new CellTable<CategoryDto>();
        selectionModel = new MultiSelectionModel<CategoryDto>();
        table.setSelectionModel(selectionModel);


        // Create name column.
        TextColumn<CategoryDto> nameColumn = new TextColumn<CategoryDto>() {
            @Override
            public String getValue(CategoryDto category) {
                return category.getName();
            }
        };
        nameColumn.setSortable(true);
        // Create category column.
        TextColumn<CategoryDto> categoryColumn = new TextColumn<CategoryDto>() {
            @Override
            public String getValue(CategoryDto category) {
                return category.getName();
            }
        };
        categoryColumn.setSortable(true);

        ButtonCell editButton = new ButtonCell();

        editColumn = new Column<CategoryDto, String>(editButton) {
            @Override
            public String getValue(CategoryDto object) {
                return "Edit";
            }
        };

        //on edit a row
        getEditColumn().setFieldUpdater(new FieldUpdater<CategoryDto, String>() {
            @Override
            public void update(int index, CategoryDto object, String value) {
                Integer id = object.getId();
                getEventBus().fireEvent(new EditCategoryEvent(id));
            }
        });

        // Add the columns.
        table.addColumn(nameColumn,
                "Nom");
        table.setColumnWidth(nameColumn,
                200, Style.Unit.PX);

        table.addColumn(editColumn,
                "modifier");

        VerticalPanel vPanel = new VerticalPanel();

        vPanel.add(hPanel);
        dataProvider = new ListDataProvider<CategoryDto>();
        SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
        simplePager = new SimplePager(SimplePager.TextLocation.CENTER, pagerResources, true, 0, true);

        simplePager.setDisplay(table);

        simplePager.setPageSize(10);

        dataProvider.addDataDisplay(table);

        vPanel.add(table);

        vPanel.add(simplePager);

        panelDecorator.add(vPanel);

        initWidget(panelDecorator);
    }

    public HasClickHandlers getAddButton() {
        return addButton;
    }

    public HasClickHandlers getDeleteButton() {
        return deleteButton;
    }

    public Column getEditColumn() {
        return editColumn;
    }

    public List<Integer> getSelectedRows() {
        List<Integer> selectedRows = new ArrayList<Integer>();

        Iterator<CategoryDto> itCategory = selectionModel.getSelectedSet().iterator();

        while (itCategory.hasNext()) {
            Integer id = itCategory.next().getId();
            selectedRows.add(id);
        }

        return selectedRows;
    }

    public void setCategories(List<CategoryDto> result) {
        dataProvider.setList(result);
    }
}
